package project.service;

import project.common.PageInfo;
import project.entity.po.User;
import project.entity.vo.UserVO;
import project.mapper.UserDao;
import project.mapper.UserDaoImpl;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class UserServiceImpl implements UserService {
    private UserDao userDao = new UserDaoImpl();

    //分页查询
    @Override
    public PageInfo pageList(Integer pageNo, Integer pageSize) {
        List<User> list = userDao.selectAllUsersPaging(pageNo, pageSize);
        PageInfo pageInfo = new PageInfo(pageNo, pageSize, list);
        pageInfo.setTotals(userDao.getCount());
        return pageInfo;
    }

    @Override
    public User getUserById(Integer id) {
        return userDao.selectUserById(id);
    }

    @Override
    public void deleteUserById(Integer id) {
        userDao.deleteUserById(id);
    }

    @Override
    public void deleteUserByName(String name) {
        userDao.deleteUserByName(name);
    }

    @Override
    public void changePassword(String name, String newPassword) {
        userDao.changePassword(name,newPassword);
    }

    //注册功能
    @Override
    public int regist(User user) {
        if (Objects.isNull(getUserByName(user.getName()))) {
            user.setCreateTime(LocalDateTime.now());
            user.setUpdateTime(LocalDateTime.now());
            return userDao.insert(user);
        } else {
            return 0;
        }
    }

    //根据召唤师名字查询他的信息
    @Override
    public User getUserByName(String name) {
        return userDao.selectUserByName(name);
    }

    //获取所有的召唤师对象
    @Override
    public List<UserVO> getAllUsers() {
        List<UserVO> list = new ArrayList<>();
        return userDao.selectAllUsers(list);
    }

    //登录，进行登录检查，并返回检查结果
    @Override
    public Boolean loginCheck(String name, String password) {
        if (Objects.isNull(getUserByName(name)))
            return false;
        else {
            return userDao.selectUserIsHaveByNameAndPassword(name, password);
        }
    }

    @Override
    public void test() {
        userDao.test();
    }
}
